<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加工艺参数' : '编辑工艺参数'" :visible.sync="addEditIsDialog_c" :close-on-click-modal="false" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="140px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数编码：" prop="code">
              <el-input v-model="ruleForm.code" placeholder="请输入参数编码" clearable maxlength="200" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入参数名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="参数分类：" prop="paramClassId">
              <el-select v-model="ruleForm.paramClassId" placeholder="请选择参数分类" clearable :disabled="seeType">
                <el-option v-for="item in paramClassArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="参数单位：" prop="unit">
              <el-input v-model="ruleForm.unit" placeholder="请输入参数单位" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数据点位：" prop="dataPointCode">
              <el-select v-model="ruleForm.dataPointCode" placeholder="请选择数据点位" filterable clearable :disabled="seeType">
                <el-option v-for="item in dataPointArray" :key="item.pointCode" :label="item.pointCode" :value="item.pointCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采集频率：" prop="collectRate">
              <el-input-number v-model="ruleForm.collectRate" placeholder="请输入采集频率" :precision="0" :step="1" :max="9999999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报警上限：" prop="alarmUpper">
              <el-checkbox label="" v-model="ruleForm.isAlarmUpperBo" :disabled="seeType">
              </el-checkbox>
              <el-input-number v-model="ruleForm.alarmUpper" placeholder="请输入报警上限" :precision="2" :step="1" :max="9999999" :min="-9999999" :disabled="seeType" style="width: 89%; margin-left: 10px"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警下限：" prop="alarmLower">
              <el-checkbox label="" v-model="ruleForm.isAlarmLowerBo" :disabled="seeType">
              </el-checkbox>
              <el-input-number v-model="ruleForm.alarmLower" placeholder="请输入报警下限" :precision="2" :step="1" :max="9999999" :min="-9999999" :disabled="seeType" style="width: 89%; margin-left: 10px"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="报警状态：" prop="alarmStatusId">
              <el-select v-model="ruleForm.alarmStatusId" placeholder="请选择报警状态" filterable clearable :disabled="seeType">
                <el-option v-for="item in alarmStatusArray" :key="item.id" :label="item.alarmName" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仪表位置：" prop="instrumentLocation">
              <el-input v-model="ruleForm.instrumentLocation" placeholder="请输入仪表位置" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否主要参数：" prop="isMainParam"><el-select v-model="ruleForm.isMainParam" placeholder="请选择是否主要参数" clearable :disabled="seeType">
                <el-option label="否" value="0"></el-option>
                <el-option label="是" value="1"></el-option> </el-select></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序号：" prop="orderNum">
              <el-input-number v-model="ruleForm.orderNum" placeholder="请输入排序号" :precision="0" :step="1" :max="9999" :min="0" :disabled="seeType"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getTechnologyDeviceParam, saveTechnologyDeviceParam, getTechnologyParamClasss } from '@/api/technology'
import { getDataPoints, getDeviceAlarmStatuss } from '@/api/device'

export default {
  components: {},
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        technologyDeviceId: '',
        code: '',
        name: '',
        paramClassId: '',
        unit: '',
        dataPointCode: '',
        collectRate: '',
        alarmLower: '',
        alarmUpper: '',
        isAlarmLowerBo: false,
        isAlarmUpperBo: false,
        isAlarmLower: '',
        isAlarmUpper: '',
        alarmStatusId: '',
        instrumentLocation: '',
        isMainParam: '',
        orderNum: ''
      },
      rules: {
        // 表单规则
        code: [{ required: true, message: '请输入参数编码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入参数名称', trigger: 'blur' }],
        paramClassId: [{ required: true, message: '请选择参数分类', trigger: 'change' }],
        unit: [{ required: false, message: '请输入参数单位', trigger: 'blur' }],
        dataPointCode: [{ required: true, message: '请选择数据点位', trigger: 'change' }],
        collectRate: [{ required: true, message: '请输入采集频率', trigger: 'blur' }],
        alarmUpper: [{ required: true, message: '请输入报警上限', trigger: 'blur' }],
        alarmLower: [{ required: true, message: '请输入报警下限', trigger: 'blur' }],
        alarmStatusId: [{ required: false, message: '请选择报警状态', trigger: 'change' }],
        instrumentLocation: [{ required: true, message: '请输入仪表位置', trigger: 'blur' }],
        isMainParam: [{ required: true, message: '请选择是否主要参数', trigger: 'change' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      paramClassArray: [], // 参数分类集合
      dataPointArray: [], // 数据点位集合
      alarmStatusArray: [] // 报警状态集合
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val
      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取工艺参数分类列表
        this.getTechnologyParamClassList()

        // 获取数据点位列表
        this.getDataPointList()

        // 获取设备报警状态列表
        this.getDeviceAlarmStatusList()
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getTechnologyDeviceParam({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.isAlarmUpperBo = res.data.isAlarmUpper != undefined && res.data.isAlarmUpper == 1
          res.data.isAlarmLowerBo = res.data.isAlarmLower != undefined && res.data.isAlarmLower == 1
          res.data.isMainParam = res.data.isMainParam != undefined ? res.data.isMainParam.toString() : ''
          this.ruleForm = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.ruleForm.technologyDeviceId = this.$route.query.technologyDeviceId
          this.ruleForm.isAlarmUpper = this.ruleForm.isAlarmUpperBo ? 1 : 0
          this.ruleForm.isAlarmLower = this.ruleForm.isAlarmLowerBo ? 1 : 0
          saveTechnologyDeviceParam(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.ruleForm.isAlarmLowerBo = false
      this.ruleForm.isAlarmUpperBo = false
      this.$parent.closeAddEdit()
    },
    // 获取工艺参数分类列表
    getTechnologyParamClassList() {
      getTechnologyParamClasss({ id: this.$route.query.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.paramClassArray = res.data
        }
      })
    },
    // 获取数据点位列表
    getDataPointList() {
      getDataPoints({ deviceCode: this.$route.query.deviceCode }).then((res) => {
        if (res.code == 200) {
          this.dataPointArray = res.data
        }
      })
    },
    // 获取设备报警状态列表
    getDeviceAlarmStatusList() {
      getDeviceAlarmStatuss({ id: this.$route.query.examinationRoomId }).then((res) => {
        if (res.code == 200) {
          this.alarmStatusArray = res.data
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
